let $host = 'https://jiayou.ziming.work/index.php?s=/api/'
let $promisify = function (api) {
  return (opt, ...arg) => {
    return new Promise((resolve, reject) => {
      api(Object.assign({}, opt, {
        success: resolve,
        fail: reject
      }), ...arg)
    })
  }
}
let $requestApi = $promisify(wx.request);
let $http = async function (opt, arg) {
  let that = this;
  let {
    showLoading = true, checkLogin = false
  } = arg;
  // 判断是否登录
  if (checkLogin) {
    let confirmLogin = that.$confirmLogin();
    if (!confirmLogin) {
      return false;
    }
  }
  // console.log(showLoading)
  // 判断是否显示loading
  if (showLoading) {
    // wx.showLoading({
    //   title: '加载中...',
    //   mask: true,
    // })
  }
  // 从opt中结构参数
  let {
    url,
    method = 'GET',
    data = {},
    header
  } = opt;
  // 配置请求地址
  opt.url = $host + url;
  // 获取个人token
  const user_token = wx.getStorageSync("user_token") || '';
  // 根据token判断请求头
  if (user_token !== '') {
    opt.header = {
      'content-type': 'application/json',
      Authorization: "Bearer " + user_token,
    }
  } else {
    opt.header = {
      'content-type': 'application/json'
    }
  }
  opt.data.wxapp_id = '10005'
  opt.data.token = user_token
  let res = await $requestApi(opt)
  wx.hideLoading();
  if (res.data.code == -1) {
    wx.navigateTo({
      url: '/pages/login/login',
    })
    return false
  }
  if (res.data.code == 1) {
    return res.data
  } else {
    if (url == 'Oilorder/noworder') {
      return false
    } else {
      wx.showToast({
        title: res.data.msg,
        icon: 'none',
        mask: false,
      })
      return false
    }
  }
};
// 微信支付
let $wxPayment = function (option) {
  let options = Object.assign({
    payment: {},
    success: () => {},
    fail: () => {},
    complete: () => {},
  }, option);
  wx.requestPayment({
    timeStamp: options.payment.timeStamp,
    nonceStr: options.payment.nonceStr,
    package: options.payment.package,
    signType: 'MD5',
    paySign: options.payment.paySign,
    success(res) {
      options.success(res);
    },
    fail(res) {
      options.fail(res);
    },
    complete(res) {
      options.complete(res);
    }
  });
}
// 取消订单
let $cancel = function (option) {
  console.log(option)
  let that = this
  wx.showModal({
    title: '提示',
    content: '确认取消订单吗？',
    success(res) {
      if (res.confirm) {
        wx.request({
          url: $host + 'order/cancelOrder',
          method: 'POST',
          data: {
            user_id: wx.getStorageSync('userid'),
            token: wx.getStorageSync('user_token'),
            order_id: option.orderid
          },
          success(res) {
            option.success(res);
          }
        })
      } else if (res.cancel) {}
    }
  })
}

//拨打电话
let $makePhone = function (e) {
  let phone = e.currentTarget.dataset.phone;
  wx.showActionSheet({
    itemList: [phone, '呼叫'],
    success: function (res) {
      if (res.tapIndex == 1) {
        wx.makePhoneCall({
          phoneNumber: phone,
          success(res) {}
        })
      }
    }
  })
}
// 显示成功提示框
let $showSuccess = function (msg, callback) {
  wx.showToast({
    title: msg,
    icon: 'none',
    mask: true,
    duration: 1500,
    success() {
      callback && (setTimeout(function () {
        callback();
      }, 1500));
    }
  });
}
//角度转弧度
let $getRad = function (d) {
  //console.log(d)
  return d * Math.PI / 180.0;
}
//根据两个经纬度获取他们之间的距离
let $getDistance = function (position1, position2) {
  let that = this;
  let lat1 = position1.lat;
  let lng1 = position1.lng;
  let lat2 = position2.lat;
  let lng2 = position2.lng;
  let EARTH_RADIUS = 6378.137; //单位M
  let PI = Math.PI;
  let radLat1 = that.$getRad(lat1);
  let radLat2 = that.$getRad(lat2);

  let a = radLat1 - radLat2;
  let b = that.$getRad(lng1) - that.$getRad(lng2);

  let s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
  s = s * EARTH_RADIUS;
  s = Math.round(s * 10000) / 10000.0;
  s = s.toFixed(2)
  return s;
}
export default {
  $promisify,
  $http,
  $wxPayment, //微信支付
  $cancel,
  $getDistance,
  $getRad,
  $makePhone,
  $showSuccess
}